var articleId=1;
$(document).ready(function() {
	getArticle();
});


/**
 * 点赞
 */
function commitPraise(e){
	var commitPraiseURL=getContextPath()+'/comment/commitpraise';
	$.post(commitPraiseURL, {'cId': e.getAttribute('cId')}, function(data, textStatus, xhr) {
		/*optional stuff to do after success */
		 getArticle(); //TODO优化
	});
}

/**
 * 获取文章
 */
function getArticle(){
	$.get(getContextPath()+'/article/getonearticle', function(data) {
		/*optional stuff to do after success */
		var obj=eval("("+data+")"); //转换json
		var article = obj.article;
		$('#content').html(article[0].articleContent);
		$('#title').html(article[0].articleName);
		articleId = article[0].articleId;
		//console.log(data+'ss');
		getComment();
	});
}


/**
 * 提交评论
 */
function comitComment(){
	var commitCommentURL=getContextPath()+"/comment/comitcomment";
	var commentContent =  $('#commentContent').val();
	$.post(commitCommentURL, {'articleId': articleId,'commentContent':commentContent}, function(data, textStatus, xhr) {
		
			
		
	});
	$('#commentContent').val('');
	$('#commentShow').html('');
	getComment();
}

function getComment(){
	var getCommentURL = getContextPath()+"/comment/getcomment";
	$.post(getCommentURL, {'articleId': articleId}, function(data, textStatus, xhr) {
		/*optional stuff to do after success */
		console.log(data);
		var tempHTML = "";
		var obj=eval("("+data+")"); //转换json
		var comment = obj.comment;
		for(var i = 0;i<comment.length;i++){
			var commitUser = comment[i].user;
			var cTime = dateFtt('MM-dd-hh:mm:ss',new Date(comment[i].commitTime));
			tempHTML+='<div class="comment-show-conclearfix"><div class="comment-show-con-imgpull-left"><img src="/file/image/'+
			commitUser.userImageUrl+'"alt="" style="width:50px;height:50px;"></div><div class="comment-show-con-listpull-leftclearfix"><div class="pl-textclearfix"><a href="javascript:;" class="comment-size-name">'
				+commitUser.userName+':</a><span class="my-pl-con">'
				+comment[i].commentContent+'</span></div><div class="date-dz"><span class="date-dz-leftpull-leftcomment-time">'
				+cTime+'</span><div class="date-dz-right pull-right comment-pl-block"><!--<a href="javascript:;"class="removeBlock">删除</a>--><a href="javascript:;" class="date-dz-plpl-hfhf-con-block pull-left">回复</a><span class="pull-left date-dz-line">|</span><a cId="'+
				comment[i].cId+'" href="javascript:;" onclick="commitPraise(this)" class="date-dz-z pull-left"><i class="date-dz-z-click-red"></i>赞(<i class="z-num">'+
				comment[i].praise+'</i>)</a></div></div><div class="hf-list-con"></div></div></div>';
		}
		$('#commentShow').html(tempHTML);

	});
}


function dateFtt(fmt,date)   
{ //author: meizz   
  var o = {   
    "M+" : date.getMonth()+1,                 //月份   
    "d+" : date.getDate(),                    //日   
    "h+" : date.getHours(),                   //小时   
    "m+" : date.getMinutes(),                 //分   
    "s+" : date.getSeconds(),                 //秒   
    "q+" : Math.floor((date.getMonth()+3)/3), //季度   
    "S"  : date.getMilliseconds()             //毫秒   
  };   
  if(/(y+)/.test(fmt))   
    fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));   
  for(var k in o)   
    if(new RegExp("("+ k +")").test(fmt))   
  fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));   
  return fmt;   
}


function getContextPath() { //获取根路径

	var pathName = document.location.pathname;
	var index = pathName.substr(1).indexOf("/");
	var result = pathName.substr(0, index + 1);
	return result;
}